package com.bignippleboy.recordthelife;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;

import android.graphics.Bitmap;

import com.bignippleboy.recordthelife.constants.Constants;
import com.bignippleboy.recordthelife.data.MapPosition;

public class PhotoManager {
	private static PhotoManager photoManager;

	private final String PHOTO_EXT = ".jpg";
//	LifeRecordItem item;

//	public LifeRecordItem getItem() {
//		return item;
//	}
//
//	public void setItem(LifeRecordItem item) {
//		this.item = item;
//	}

	public static PhotoManager getInstance() {
		if (photoManager == null)
			photoManager = new PhotoManager();
		return photoManager;
	}

	public String savePicture(Bitmap bm) {

		Date curDate = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String dateString = sdf.format(curDate);
		sdf = new SimpleDateFormat("hhmmss");
		String timeString = sdf.format(curDate);

		String basicPathString = Constants.BASIC_PATH + dateString;
		if (Constants.isSDcardWork()) {
			File path = new File(basicPathString);
			if (!path.exists())
				path.mkdirs();
		}

		String photoPathString = basicPathString+"/"+timeString+PHOTO_EXT;
//		if(center != null)
//			Constants.item.setPosition(center);
		File captureFile = new File(photoPathString);
		try {
			if (!captureFile.exists())
				captureFile.createNewFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(Constants.m_MyDataBaseAdapter == null)
			System.out.println("Constants.m_MyDataBaseAdapter=null............");
		
		long rowId;
//		if (captureFile != null) {
//			rowId = Constants.m_MyDataBaseAdapter.insertData(Constants.item);
//			Constants.item.setRowId(rowId);
//		}
//		Constants.m_MyDataBaseAdapter.close();
		FileOutputStream bos;
		try {
			bos = new FileOutputStream(captureFile);
			bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
			bos.flush();
			bos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		Constants.m_MyDataBaseAdapter.close();
		
		return photoPathString;
	}

}
